package top.buyibuyi.course.controller.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.buyibuyi.course.R;
import top.buyibuyi.course.pojo.Admin;
import top.buyibuyi.course.service.login.LoginService;

import java.util.UUID;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class LoginController {

    @Autowired
    private LoginService loginService;
    public Admin admin1;
    @PostMapping("/login")
    @ResponseBody
    public R loginAdmin(@RequestBody Admin admin){
        System.out.println(admin);
        try {
            admin1= loginService.loginAdmin(admin);
            if(admin1 == null){
                System.out.println("查询之后"+admin1);
                return R.error();
            }else {
                System.out.println("查询之后"+admin1);
                String uuid = UUID.randomUUID().toString().replaceAll("-","");
                return R.ok().data("token",uuid);
            }
        }catch (Exception e){
            return R.error();
        }
    }

    @GetMapping("/info")
    public R info(){
        return R.ok().data("roles",admin1).data("name",admin1.getName()).data("avatar",admin1.getAvatar());
    }
}
